﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App.Utils
{

    public class Result
    {
        public string Msg { get; set; }

        /// <summary>
        /// 0成功，其他失败
        /// </summary>
        public ResultCode Code { get; set; } = 0;

        public bool IsSuccess
        {
            get { return Code == ResultCode.Success; }
        }

        public bool IsError
        {
            get { return !IsSuccess; }
        }

        public Result ToError()
        {
            Code = ResultCode.Error;
            return this;
        }

        public Result ToError(string msg)
        {
            Msg = msg;
            Code = ResultCode.Error;
            return this;
        }

        public Result ToSuccess()
        {
            Code = ResultCode.Success;
            return this;
        }

        public Result ToSuccess(string msg)
        {
            Msg = msg;
            Code = ResultCode.Success;
            return this;
        }

    }

    public class Result<T>
    {
        public string Msg { get; set; }

        public T Data { get; set; }

        /// <summary>
        /// 0成功，其他失败
        /// </summary>
        public ResultCode Code { get; set; } = 0;

        public bool IsSuccess
        {
            get { return Code == ResultCode.Success; }
        }

        public bool IsError
        {
            get { return !IsSuccess; }
        }

        public Result<T> ToError()
        {
            Code = ResultCode.Error;
            return this;
        }

        public Result<T> ToError(string msg)
        {
            Msg = msg;
            Code = ResultCode.Error;
            return this;
        }

        public Result<T> ToSuccess()
        {
            Code = ResultCode.Success;
            return this;
        }

        public Result<T> ToSuccess(string msg)
        {
            Msg = msg;
            Code = ResultCode.Success;
            return this;
        }

    }

    public enum ResultCode
    {
        Success = 0,
        Error = 1
    }
}
